<?php

require_once 'BasicController.php';
require_once '../application/business/TransactionBusiness.php'; 	

class ErrorController extends BasicController
{

	public function init ()
	{
		$ad = new AdminBusiness();
		$user = Zend_Auth::getInstance()->getStorage()->read();
		$this->view->user = $user;

		if(isset($user) && $user != null){
			$cartBusiness = new CartBusiness();
			$this->view->shopping_count = $cartBusiness->countItems($user['idClient']);
		}

		$this->view->enterprises = $ad->getAllEnterprises();
		$this->view->categories = $ad->getAllCategories();

	}

	public function errorAction()
	{
		$errors = $this->_getParam('error_handler');

		if (!$errors || !$errors instanceof ArrayObject) {
			$this->view->message = 'You have reached the error page';
			return;
		}
		

		if($this->getRequest()->getParam('error') == '404'){
			// application error
				$this->getResponse()->setHttpResponseCode(500);
				$priority = Zend_Log::CRIT;
				$this->view->message = '<div id="thanks">
											<div style="display: inline-block; border: 1px solid rgb(75, 2, 2); height: 110px; width: 100%; background: rgb(253, 213, 213); text-align: justify;">
												<div style="margin: 0 auto; height: 100px; width: 60%">
													<div style="float: left; background: url('.'../images/error.png'.'); width: 50px; height: 50px; margin: 25px 0 0 10px;"></div>
													<div style="float: left; margin: 30px 0 0 10px; font-size: 40px">
														Ops! Página não encontrada.
													</div>
												</div>
											</div>
										</div>';
				return;
		}
		
		switch ($errors->type) {
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
				// 404 error -- controller or action not found
				$this->getResponse()->setHttpResponseCode(404);
				$priority = Zend_Log::NOTICE;
				$this->view->message = '<div id="thanks">
											<div style="display: inline-block; border: 1px solid rgb(75, 2, 2); height: 110px; width: 100%; background: rgb(253, 213, 213); text-align: justify;">
												<div style="margin: 0 auto; height: 100px; width: 60%">
													<div style="float: left; background: url('.'../images/error.png'.'); width: 50px; height: 50px; margin: 25px 0 0 10px;"></div>
													<div style="float: left; margin: 30px 0 0 10px; font-size: 40px">
														Ops! Página não encontrada.
													</div>
												</div>
											</div>
										</div>';
				break;
			default:
				// application error
				$this->getResponse()->setHttpResponseCode(500);
				$priority = Zend_Log::CRIT;
				$this->view->message = '<div id="thanks">
											<div style="display: inline-block; border: 1px solid rgb(75, 2, 2); height: 175px; width: 100%; background: rgb(253, 213, 213); text-align: justify;">
												<div style="margin: 0 auto; height: 100px; width: 60%">
													<div style="float: left; background: url('.'../images/error.png'.'); width: 50px; height: 50px; margin: 65px 0 0 10px;"></div>
													<div style="float: left; margin: 30px 0 0 10px; font-size: 40px">
														Ops! Ocorreu algum erro. <br /><br />Tente novamente mais tarde!
													</div>
												</div>
											</div>
										</div>';
				break;
		}

		// Log exception, if logger available
		if ($log = $this->getLog()) {
			$log->log($this->view->message, $priority, $errors->exception);
			$log->log('Request Parameters', $priority, $errors->request->getParams());
		}

		// conditionally display exceptions
		if ($this->getInvokeArg('displayExceptions') == true) {
			$this->view->exception = $errors->exception;
		}

		$this->view->request   = $errors->request;
	}

	public function getLog()
	{
		$bootstrap = $this->getInvokeArg('bootstrap');
		if (!$bootstrap->hasResource('Log')) {
			return false;
		}
		$log = $bootstrap->getResource('Log');
		return $log;
	}


}

